package cn.lysj.framework.foodv2.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.lysj.framework.base.BaseController;

import cn.lysj.framework.common.utils.random.RandomUtil;
import cn.lysj.framework.common.utils.web.ParamUtil;
import cn.lysj.framework.common.utils.web.Resp;
import cn.lysj.framework.foodv2.domain.Food;
import cn.lysj.framework.foodv2.domain.StandardLevOne;
import cn.lysj.framework.foodv2.domain.StandardLevTwo;
import cn.lysj.framework.foodv2.service.FoodService;
import cn.lysj.framework.foodv2.service.StandardLevOneService;
import cn.lysj.framework.foodv2.service.StandardLevTwoService;
import cn.lysj.framework.foodv2.vo.StandVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;


/**
 * @author drj
 * @description 一级规格控制层
 * @create 2018-11-09 11:29:51
 **/
@Slf4j
@RestController
@Api(description = "一级规格管理接口")
@RequestMapping("/standard_lev_one")
public class StandardLevOneController extends BaseController<StandardLevOne> {

    @Resource
    private StandardLevOneService standardLevOneService;

    @Resource
    private FoodService foodService;

    @Override
    public StandardLevOneService getService() {
        return standardLevOneService;
    }


    @Override
    public Resp save(StandardLevOne food) {
        return null;
    }


    /**
     * @author drj
     * @date 2018/11/11 16:41
     * @Description:添加菜品对应的规格信息
     */
//    @PostMapping("/save_rewrite")
//    public Resp save(String foodId, String standardInfo) {
//        if (ParamUtil.isBlank(foodId)) {
//            return new Resp(Resp.PARAM_ERROR, "请选择菜品");
//        }
//        //删除菜品对应的一级规格和二级规格信息
//        List<String> list = new ArrayList<>();
//        List<StandardLevOne> standardLevOneList = standardLevOneService.getRepository().findByFoodId(foodId);
//        for (StandardLevOne standardLevOne : standardLevOneList) {
//            list.add(standardLevOne.getId());
//        }
//        String[] strings = list.toArray(new String[list.size()]);
//        delete(strings);
//        //添加菜品信息
//        if (standardInfo.contains(";")) {
//            String[] itemArray = standardInfo.split(";");
//            for (String s : itemArray) {
//                String[] itemArray1 = s.split("-");
//                StandardLevOne standardLevOne = new StandardLevOne();
//                standardLevOne.setFlag(itemArray1[0]);
//                standardLevOne.setFoodId(foodId);
//                standardLevOne.setName(itemArray1[1]);
//                standardLevOne.setUpdateTime(new Date());
//                standardLevOne = standardLevOneService.getRepository().save(standardLevOne);
//                for (int i = 2; i < itemArray1.length; i++) {
//                    StandardLevTwo standardLevTwo = new StandardLevTwo();
//                    standardLevTwo.setStandardLevOneId(standardLevOne.getId());
//                    standardLevTwo.setName(itemArray1[i]);
//                    standardLevTwo.setMoney(BigDecimal.ZERO);
//                    standardLevTwoService.getRepository().save(standardLevTwo);
//                }
//            }
//        }
//        return new Resp("添加成功");
//    }


    /**
     * @author drj
     * @date 2018/11/29 14:37
     * @Description:添加一级规格
     */
    @PostMapping("save_rewrite")
    public Resp saveRewrite(StandardLevOne model) {
        if (ParamUtil.isBlank(model.getFoodId())) {
            return new Resp(Resp.PARAM_ERROR, "参数错误");
        }
        Food food = foodService.findOne(model.getFoodId());
        if (null == food) {
            return new Resp(Resp.PARAM_ERROR, "参数错误");
        }
        if (ParamUtil.isBlank(model.getFlag())) {
            return new Resp(Resp.PARAM_ERROR, "请选择标识");
        }
        if (ParamUtil.isBlank(model.getName())) {
            return new Resp(Resp.PARAM_ERROR, "请填写一级规格名称");
        }
        StandardLevOne standardLevOne = new StandardLevOne();
        standardLevOne.setFoodId(model.getFoodId());
        standardLevOne.setFlag(model.getFlag());
        standardLevOne.setName(model.getName());
        standardLevOne = standardLevOneService.save(standardLevOne);
        return new Resp(standardLevOne);
    }


    /**
     * @author drj
     * @date 2018/11/11 16:41
     * @Description:显示菜品规格信息
     */
    @GetMapping("/list_rewrite")
    public Resp save(String foodId) {
        return new Resp(standardLevOneService.getStandVo(foodId));
    }


}


